package us.sunrisemorning.pixivbrower.web;

import com.jfinal.aop.Aop;
import com.jfinal.config.*;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.template.Engine;
import us.sunrisemorning.pixivbrower.DevMode;
import us.sunrisemorning.pixivbrower.model._MappingKit;
import us.sunrisemorning.pixivbrower.spider.DbQueue;
import us.sunrisemorning.pixivbrower.web.controller.BrowerController;
import us.sunrisemorning.pixivbrower.web.controller.ConfigController;
import us.sunrisemorning.pixivbrower.web.controller.MainController;
import us.sunrisemorning.pixivbrower.web.controller.SearchController;
import us.sunrisemorning.pixivbrower.web.pulgin.FlywayPlugin;

public class WebConfig extends JFinalConfig {

    @Override
    public void configConstant(Constants me) {
        me.setDevMode(DevMode.SWITCH);
        me.setInjectDependency(true);
    }

    @Override
    public void configRoute(Routes me) {
        me.setBaseViewPath("_view");
        me.add("/", MainController.class);
        me.add("/brower", BrowerController.class);
        me.add("/search", SearchController.class);
        me.add("/config", ConfigController.class);
    }

    @Override
    public void configEngine(Engine me) {
    }

    @Override
    public void configPlugin(Plugins me) {
        C3p0Plugin c3p0Plugin = new C3p0Plugin("jdbc:h2:./pixivbrower.db", null, null, "org.h2.Driver");

        FlywayPlugin flywayPlugin = new FlywayPlugin(c3p0Plugin);

        ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin(c3p0Plugin);
        _MappingKit.mapping(activeRecordPlugin);

        me.add(c3p0Plugin);
        me.add(flywayPlugin);
        me.add(activeRecordPlugin);
    }

    @Override
    public void configInterceptor(Interceptors me) {
    }

    @Override
    public void configHandler(Handlers me) {
    }

    @Override
    public void onStart() {
        DbQueue queue = Aop.get(DbQueue.class);
        queue.start();
    }
}
